﻿#include "CAlarmDetailsDlg.h"
#include "cimagedownloader.h"
#include "Global.h"
#include "CZoomableLabel.h"
#include "CSqlOper.h"
CAlarmDetailsDlg::CAlarmDetailsDlg(CAlarmPoint *pAlarmPoint, QWidget *parent)
	:QDialog(parent)
{
	ui.setupUi(this);
	m_pAlarmPoint = pAlarmPoint;
	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
	this->setWindowTitle(A2T("报警详情"));
	// 获取 Qt 窗口的原生 HWND 句柄
	HWND hwnd = reinterpret_cast<HWND>(this->winId());
	setNativeTitleBarColor(hwnd, RGB(56, 63, 81));
	// 确保labelImg能够接收事件
	ui.labelImg->setFocusPolicy(Qt::StrongFocus);
	ui.labelImg->setMouseTracking(true);
	setStyleSheetFile();
	initConnect();
	setData();
}

CAlarmDetailsDlg::~CAlarmDetailsDlg()
{}

void CAlarmDetailsDlg::initConnect()
{
	connect(ui.pushButtonAlarmConfrim, &QPushButton::clicked, this, &CAlarmDetailsDlg::slotPushBtnAlarmConfrim);
	connect(ui.pushButtonClose, &QPushButton::clicked, this, &CAlarmDetailsDlg::slotPushBtnClose);
}

void CAlarmDetailsDlg::setStyleSheetFile()
{
	QString sQssPath = ":/qss/styleSheet.css";
	QFile qss(sQssPath);
	qss.open(QFile::ReadOnly);
	this->setStyleSheet(qss.readAll());
}

void CAlarmDetailsDlg::setData()
{
	ui.lineEditCamerName->setText(m_pAlarmPoint->getName());
	QString sAlarmLevel = A2T("一级");
	QString sAlarmLevelDesc = A2T("报警消失未确认");
	if (m_pAlarmPoint->getAlarmLevel() == EAlarmLevel::enLow || m_pAlarmPoint->getAlarmLevel() == EAlarmLevel::enHigh)
	{
		sAlarmLevel = A2T("一级");

	}
	else if (m_pAlarmPoint->getAlarmLevel() == EAlarmLevel::enHighHigh || m_pAlarmPoint->getAlarmLevel() == EAlarmLevel::enLowLow)
	{
		sAlarmLevel = A2T("二级");
	}
	ui.lineEditAlarmLevel->setText(sAlarmLevel);
	ui.lineEditAlarmTime->setText(m_pAlarmPoint->getLastUpdateTime().toString("yyyy/MM/dd hh:mm:ss"));
	ui.lineEditAlarmType->setText(A2T("工艺报警"));
	if (m_pAlarmPoint->getAlarmConfirmStatus() == EAlarmConfirmStatus::enAlarmPending)
	{
		ui.lineEditIsConfrim->setText(A2T("否"));
	}
	else
	{
		ui.lineEditIsConfrim->setText(A2T("是"));
	}
	QString sAlarmDesc = m_pAlarmPoint->getAlarmDesc();
	if (sAlarmDesc == "")
	{
		sAlarmDesc = m_pAlarmPoint->getName() + A2T("报警");
	}
	ui.lineEditAlarmDesc->setText(sAlarmDesc);
	QString sUrl = m_pAlarmPoint->getFileRef();
	QString username = "admin";
	QString password = "ky002380"; // 替换为实际密码

	// 创建下载器
	CImageDownloader* downloader = new CImageDownloader(ui.labelImg, sUrl, username, password, this);

	// 关键修改：确保使用CZoomableLabel的setPixmap方法
	connect(downloader, &CImageDownloader::downloadFinished, this, [this](const QPixmap& pixmap) {
		qDebug() << A2T("图片下载完成，准备设置到CZoomableLabel");

		// 将ui.labelImg转换为CZoomableLabel
		CZoomableLabel* zoomLabel = qobject_cast<CZoomableLabel*>(ui.labelImg);
		if (zoomLabel) {
			qDebug() << A2T("成功转换为CZoomableLabel，调用setPixmap");
			zoomLabel->setPixmap(pixmap);

			// 确保控件获得焦点，以便接收事件
			zoomLabel->setFocus();
		}
		else {
			qWarning() << A2T("转换CZoomableLabel失败! 实际类型:") << ui.labelImg->metaObject()->className();
			// 备用：直接设置到QLabel
			ui.labelImg->setPixmap(pixmap.scaled(ui.labelImg->size(), Qt::KeepAspectRatio, Qt::SmoothTransformation));
		}
	});

	connect(downloader, &CImageDownloader::downloadFailed, this, [](const QString& error) {
		qWarning() << "图片下载失败:" << error;
	});

	// 开始下载
	downloader->downloadAsync();
	
}

void CAlarmDetailsDlg::slotPushBtnAlarmConfrim()
{
	QString sErrMsg = "";
	m_pAlarmPoint->setAlarmConfirm();
	QMessageBox::information(nullptr, QString::fromLocal8Bit("信息"), A2T("报警确认成功"));
	QString sErr = "";
	updataPointInfo(m_pAlarmPoint, sErr);
	m_pAlarmPoint->setAlarmStateChange(false);
}

void CAlarmDetailsDlg::slotPushBtnClose()
{
	this->close();
}
